<?php

namespace app\index\model;

use think\Model;
use think\Db;

class UserModel extends Model {

    //用户好友分组 
    function friends_group_list($uid) {
        $where['user_id'] = $uid;
        return Db::table('chat_user_friends_groups')->field('id,group_name')->where($where)->select();
    }

    //用户好友分组 
    function friends_list($uid) {
        $where['user_id'] = $uid;
        return Db::table('chat_user_friends f')
                        ->field('f.friends_id,f.note_nickname,f.friends_group,nick_name,header,remark')->join('chat_user u', 'u.id = f.friends_id', 'left')->where($where)->select();
    }

    //用户 群聊天列表
    function group_list($uid) {
        $where['user_id'] = $uid;
        return Db::table('chat_group_user u')
                        ->field('g.group_name as groupname,g.id,group_ico as avatar')->join('chat_group g', 'u.group_id = g.id', 'left')->where($where)->order('u.create_time desc')->select();
    }

    //用户个人信息
    function get_user_info($uid) {
        $where['id'] = $uid;
        return Db::table('chat_user')->field('id,header,nick_name,remark')->where($where)->find();
    }

    //获取群信息
    function get_group_info($group_id) {
        $where['id'] = $group_id;
        return Db::table('chat_group')->where($where)->find();
    }

    function get_group_list($group_id) {
        $where['group_id'] = $group_id;
        return Db::table('chat_group_user g')->field('u.nick_name as username,u.id,u.header as avatar,u.remark as sign')
                        ->join('chat_user u', 'u.id = g.user_id', 'left')
                        ->where($where)
                        ->select();
    }

//    public $results = array('status' => 'error', 'msg' => '', 'data' => '');
//
//    function user_list($key = "") {
//        $pageParam['query']['key'] = $key;
//        if ($key) {
//            $where['user_name'] = ['like', "%$key%"];
//            $where['email'] = ['like', "%$key%"];
//        }
//        $rs = Db::table('user')->whereOr($where)->paginate(10, FALSE, $pageParam);
//        if (!$rs) {
//            return $this->results;
//        }
//        $this->results['status'] = 'success';
//        $this->results['data'] = $rs;
//        return $this->results;
//    }
//
//    function user_info($id) {
//        $where['id'] = $id;
//        return Db::table('user')->where($where)->find();
//    }
//
//    function change_user($data) {
//        $where['id'] = $data["id"];
//        $rs = Db::table('user')->where($where)->update($data);
//        return $rs;
//    }
//
//    function del_user($id) {
//        $where['id'] = $id;
//        return Db::table('user')->where($where)->delete();
//    }
//
//    /**
//     * 封号 /解封
//     * @param type $user_id
//     * @param type $state
//     */
//    function banned($user_id, $state) {
//        if ($state == 1) {
//            $up['status'] = "1";
//        } else {
//            $up['status'] = "0";
//        }
//        return Db::table('user')->where('id', $user_id)->update($up);
//    }
}
